//
//  QuestionAnswerController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/1.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "QuestionAnswerController.h"
#import "QAModel.h"

@interface QuestionAnswerController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView * tableview;

@property (nonatomic , assign) NSInteger index;
@property (nonatomic , strong) NSMutableArray * dataArray;

@end

@implementation QuestionAnswerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(void)setNavi
{
    self.title = @"购物相关";
}

- (void)setUI
{
    self.index = -1;
    self.dataArray = [NSMutableArray array];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableview.tableFooterView = [UIView new];
}

-(void)setData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"help_category_id":[NSString stringWithFormat:@"%ld",self.categoryID]};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@help-center/sub-index",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            for (int i = 0; i < [[responseObject objectForKey:@"data"] count]; i ++) {
                QAModel * model = [[QAModel alloc]init];
                [model setValuesForKeysWithDictionary:[responseObject objectForKey:@"data"][i]];
                [self.dataArray addObject:model];
            }
            [self.tableview reloadData];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (self.dataArray.count > indexPath.section) {
        QAModel * model = self.dataArray[indexPath.section];
        cell.textLabel.text = model.contents;
        cell.textLabel.font = FontSize(13);
        cell.contentView.backgroundColor = Color(250, 250, 250);
    }
    cell.textLabel.numberOfLines = 0;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.index == section) {
        return 1;
    }else
    {
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    btn.titleLabel.font = FontSize(14);
    btn.tag = 1001 + section;
    if (self.dataArray.count > section) {
        QAModel * model = self.dataArray[section];
        [btn setTitle:model.help_name forState:(UIControlStateNormal)];
    }
    btn.backgroundColor = [UIColor whiteColor];
    btn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(reloadCell:) forControlEvents:(UIControlEventTouchUpInside)];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(15, 49, kScreenWidth - 15, 1)];
    view.backgroundColor = TextLessImportColor;
    [btn addSubview:view];
    
    return btn;
}

- (void)reloadCell:(UIButton *)sender
{
    self.index = sender.tag - 1001;
    [self.tableview reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
